% this reads in the ADV log files, saves the data in a .MAT format 


function datastr = convertADVlog_profile2MAT_NORTEK_v2(filename)
 
fname = (filename);

data2=load ([(fname) '.sen']);
    

fid=fopen([(fname) '.dat']);
count = 1;
while ~feof(fid)

t2=fgetl(fid);

val=str2num(t2);


%test for same number of values on each line
if length(val)==29 %changes from 29 to 38 because there are more columns, maybe from the IMU 
%if length(val)==38 %changes from 29 to 38 because there are more columns, maybe from the IMU 
%if length(val)==18    
 data(count,:)=val;
    count = count+1;
end


end



burstno=data(:,1);
recordno=data(:,2);
Vx=data(:,3)*100;   %velocity in cm/sec
Vy=data(:,4)*100;
Vz=data(:,5)*100;
A1=data(:,6);       %signal amplitude in counts  
A2=data(:,7);
A3=data(:,8);
S1=data(:,9);       %Signal to Noise ration (dB)
S2=data(:,10);
S3=data(:,11);
C1=data(:,12);       %correlation score expressed as a percentage
C2=data(:,13);
C3=data(:,14);
%heading=data(:,16);       % degree 
heading=interp(data2(:,11),round(size(recordno,1)/size(data2,1))); %edited KAF Sept 2014. Need to use interp b/c values aren't sampled as frequently as the other bursts are.
%pitch=data(:,17);
pitch=interp(data2(:,12),round(size(recordno,1)/size(data2,1)));%
%roll=data(:,18);
roll=interp(data2(:,13),round(size(recordno,1)/size(data2,1)));
%ADVtemp=data(:,20);    % degree C
ADVtemp=interp(data2(:,14),round(size(recordno,1)/size(data2,1))); %deg C
ADVpress=data(:,15);    % dbar

eval (['save ' fname '  Vx Vy Vz A1 A2 A3 C1 C2 C3 heading pitch roll ADVtemp ADVpress '])


figure (100)
plot (-data(:,15))
[x y]=ginput;
x=round(x);


proVx=data(x(1):x(2),2)/100;   
proVy=data(x(1):x(2),3)/100;
proVz=data(x(1):x(2),4)/100;
proA1=data(x(1):x(2),5);      
proA2=data(x(1):x(2),6);
proA3=data(x(1):x(2),7);
proC1=data(x(1):x(2),8);       
proC2=data(x(1):x(2),9);
proC3=data(x(1):x(2),10);
%pro_heading=data(x(1):x(2),11)/10;
pro_heading=heading(x(1):x(2),:)/10;
%pro_pitch=data(x(1):x(2),12)/10;
pro_pitch=pitch(x(1):x(2),:)/10;
%pro_roll=data(x(1):x(2),13)/10;
pro_roll=roll(x(1):x(2),:)/10;
%pro_ADVtemp=data(x(1):x(2),14)/100; 
pro_ADVtemp=ADVtemp(x(1):x(2),:)/100;
pro_ADVpress=data(x(1):x(2),15);



eval (['save ' fname '-pro  proVx proVy proVz proA1 proA2 proA3 proC1 proC2 proC3 pro_heading pro_pitch pro_roll pro_ADVtemp pro_ADVpress '])

eval (['save ' fname 'index.asc x -ascii'])  %1st 2 are profile start and stop times and rest are TSS depth start and stop times


    
%fclose(fid);

clear Vx Vy Vz A1 A2 A3 C1 C2 C3 heading pitch roll temp pressure
clear proVx proVy proVz proA1 proA2 proA3 proC1 proC2 proC3 pro_heading pro_pitch pro_roll pro_ADVtemp pro_ADVpress
clear x
end
